package com.zyd.shiro.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyd.shiro.business.entity.WithdrawalLine;
import com.zyd.shiro.business.service.PakWithdrawalLineService;
import com.zyd.shiro.framework.object.BaseConditionVO;
import com.zyd.shiro.persistence.beans.PakWithdrawalLine;
import com.zyd.shiro.persistence.mapper.PakWithdrawalLineMapper;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Slf4j
@Service
public class PakWithdrawalLineServiceImpl implements PakWithdrawalLineService {

    @Autowired
    private PakWithdrawalLineMapper pakWithdrawalLineMapper;

    @Override
    public WithdrawalLine insert(WithdrawalLine withdrawalLine) {
        Assert.notNull(withdrawalLine, "对象不可为空！");

        pakWithdrawalLineMapper.insertSelective(withdrawalLine.getPakWithdrawalLine());
        return withdrawalLine;
    }

    @Override
    public void insertList(List<WithdrawalLine> withdrawalLines) {
            for(WithdrawalLine withdrawalLine:withdrawalLines){
               pakWithdrawalLineMapper.insertSelective(withdrawalLine.getPakWithdrawalLine());
           }
    }


    /**
     * 根据主键字段进行删除，方法参数必须包含完整的主键属性
     *
     * @param primaryKey
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByPrimaryKey(Long primaryKey) {
        return pakWithdrawalLineMapper.deleteByPrimaryKey(primaryKey) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(WithdrawalLine withdrawalLine) {
        Assert.notNull(withdrawalLine, "对象不可为空！");
        withdrawalLine.setUpdateTime(new Date());
        return pakWithdrawalLineMapper.updateByPrimaryKey(withdrawalLine.getPakWithdrawalLine()) > 0;
    }

    @Override
    public boolean updateSelective(WithdrawalLine withdrawalLine) {
        Assert.notNull(withdrawalLine, "对象不可为空！");
        withdrawalLine.setUpdateTime(new Date());
        return pakWithdrawalLineMapper.updateByPrimaryKeySelective(withdrawalLine.getPakWithdrawalLine()) > 0;
    }

    /**
     * 根据主键字段进行查询，方法参数必须包含完整的主键属性，查询条件使用等号
     *
     * @param primaryKey
     * @return
     */

    @Override
    public WithdrawalLine getByPrimaryKey(Long primaryKey) {
        PakWithdrawalLine pakWithdrawalLine = pakWithdrawalLineMapper.selectByPrimaryKey(primaryKey);
        return null == pakWithdrawalLine ? null : new WithdrawalLine(pakWithdrawalLine);
    }


    /**
     * 根据实体中的属性进行查询，只能有一个返回值，有多个结果时抛出异常，查询条件使用等号
     *
     * @param
     * @return
     */
    @Override
    public WithdrawalLine getOneByEntity(WithdrawalLine test) {
        Assert.notNull(test, "对象不可为空！");
        PakWithdrawalLine pakWithdrawalLine = pakWithdrawalLineMapper.selectOne(test.getPakWithdrawalLine());
        return null == pakWithdrawalLine ? null : new WithdrawalLine(pakWithdrawalLine);
    }

    @Override
    public List<WithdrawalLine> listAll() {
        List<PakWithdrawalLine> pakWithdrawalLines = pakWithdrawalLineMapper.selectAll();
        if (CollectionUtils.isEmpty(pakWithdrawalLines)) {
            return null;
        }
        List<WithdrawalLine> withdrawalLines = new ArrayList<>();
        for (PakWithdrawalLine pakWithdrawalLine : pakWithdrawalLines) {
            withdrawalLines.add(new WithdrawalLine(pakWithdrawalLine));
        }
        return withdrawalLines;
    }

    @Override
    public List<WithdrawalLine> listByEntity(WithdrawalLine entity) {
        return null;
    }


    /**
     * 分页查询
     *
     * @param vo
     * @return
     */
    @Override
    public PageInfo<WithdrawalLine> findPageBreakByCondition(BaseConditionVO vo) {
        PageHelper.startPage(vo.getPageNumber(), vo.getPageSize());
        List<PakWithdrawalLine> pakWithdrawalLines = pakWithdrawalLineMapper.findPageBreakByCondition(vo);
        if (CollectionUtils.isEmpty(pakWithdrawalLines)) {
            return null;
        }
        List<WithdrawalLine> withdrawalLines = new ArrayList<>();
        for (PakWithdrawalLine pakWithdrawalLine : pakWithdrawalLines) {
            withdrawalLines.add(new WithdrawalLine(pakWithdrawalLine));
        }
        PageInfo bean = new PageInfo<PakWithdrawalLine>(pakWithdrawalLines);
        bean.setList(withdrawalLines);
        return bean;
    }
@Override
    public void increase(PakWithdrawalLine pakWithdrawalLine) {
        pakWithdrawalLineMapper.insert(pakWithdrawalLine);
    }

    @Override
    public List<PakWithdrawalLine> findByWithdrawal(Long withdrawalId) {
        return pakWithdrawalLineMapper.findByWithdrawal(withdrawalId);
    }


}
